package com.woniuxy.joylifecenter.service.impl;

import com.woniuxy.common.entity.CenterAuth;
import com.woniuxy.common.entity.CenterAuthExample;
import com.woniuxy.common.entity.CenterAuth;
import com.woniuxy.common.entity.CenterAuthExample;
import com.woniuxy.common.mapper.CenterAuthMapper;
import com.woniuxy.common.mapper.CenterAuthMapper;
import com.woniuxy.joylifecenter.exception.CenterException;
import com.woniuxy.joylifecenter.service.AuthService;
import com.woniuxy.joylifecenter.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    CenterAuthMapper centerAuthMapper;


    /**
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/8
     * @Param [centerAuth]
     * @Description 添加权限
     **/
    @Override
    public int addAuth(CenterAuth centerAuth) {
        if (queryAuthByName(centerAuth).size() == 0) {
            return centerAuthMapper.insertSelective(centerAuth);
        } else {
            throw new CenterException("该权限已存在，请勿重复添加");
        }
    }

    /**
     * @return java.util.List<com.woniuxy.common.entity.CenterAuth>
     * @Author ArmWatt
     * @Date 2020/5/8
     * @Param [centerAuth]
     * @Description 添加权限验重
     **/
    @Override
    public List<CenterAuth> queryAuthByName(CenterAuth centerAuth) {
        CenterAuthExample example = new CenterAuthExample();
        CenterAuthExample.Criteria criteria = example.createCriteria();
        criteria.andCenterAuthNameEqualTo(centerAuth.getCenterAuthName());
        return centerAuthMapper.selectByExample(example);

    }

    /**
     * @return java.util.List<com.woniuxy.common.entity.CenterAuth>
     * @Author ArmWatt
     * @Date 2020/5/7
     * @Param [centerAuth]
     * @Description 多条件权限模糊查询
     **/
    @Override
    public List<CenterAuth> queryAuthByCondition(CenterAuth centerAuth) {
        CenterAuthExample example = new CenterAuthExample();
        CenterAuthExample.Criteria criteria = example.createCriteria();
        if (centerAuth.getCenterAuthid() != null) {
            criteria.andCenterAuthidLike("%" + centerAuth.getCenterAuthid() + "%");
        }
        if (centerAuth.getCenterAuthName() != null) {
            criteria.andCenterAuthNameLike("%" + centerAuth.getCenterAuthName() + "%");
        }
        if (centerAuth.getCenterAuthUrls() != null) {
            criteria.andCenterAuthUrlsLike("%" + centerAuth.getCenterAuthUrls() + "%");
        }
        return centerAuthMapper.selectByExample(example);

    }

    /**
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/8
     * @Param [centerAuth]
     * @Description 编辑权限
     **/
    @Override
    public int editAuth(CenterAuth centerAuth) {

        if (editVerify(centerAuth) == 0) {
            return centerAuthMapper.updateByPrimaryKey(centerAuth);
        } else {
            throw new CenterException("该权限已存在");
        }

    }

    /**
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/8
     * @Param [centerAuth]
     * @Description 编辑权限去重
     **/
    @Override
    public int editVerify(CenterAuth centerAuth) {
        CenterAuthExample example = new CenterAuthExample();
        CenterAuthExample.Criteria criteria = example.createCriteria();
        criteria.andCenterAuthidNotEqualTo(centerAuth.getCenterAuthid());
        criteria.andCenterAuthNameEqualTo(centerAuth.getCenterAuthName());
        return centerAuthMapper.selectByExample(example).size();
    }

    /**
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/8
     * @Param [AuthId]
     * @Description 编辑单个权限
     **/
    @Override
    public int deleteAuth(String AuthId) {
        int count = centerAuthMapper.deleteByPrimaryKey(AuthId);
        if (count != 1) {
            throw new CenterException("删除失败，请勿重复删除");
        }
        return count;
    }

    /**
     * @return int
     * @Author ArmWatt
     * @Date 2020/5/7
     * @Param [AuthIds]
     * @Description 批量删除角色
     **/
    @Override
    public int batchDeleteAuth(List<String> AuthIds) {
        CenterAuthExample example = new CenterAuthExample();
        CenterAuthExample.Criteria criteria = example.createCriteria();
        criteria.andCenterAuthidIn(AuthIds);
        int count = centerAuthMapper.deleteByExample(example);
        if (count == 0) {
            throw new CenterException("批量删除失败，请勿重复删除");
        }
        return count;
    }


}

